import * as turf from "@turf/turf";
import GeoJSON from "ol/format/GeoJSON.js";

/**
 * @abstract 生成当前地图范围内的随机点坐标
 * @param {*} map
 * @param {number} count  坐标数量
 * @returns
 */
export const randomPosition = (map, count, extent) => {
  const positions = [];
  const currentExtent = extent || map.getView().calculateExtent(map.getSize());

  for (let index = 0; index < count; index++) {
    positions.push(turf.randomPosition(currentExtent));
  }

  return positions;
};

/**
 *@abstract 生成当前地图范围内的随机点数据
 * @param {*} map
 * @param {number} count 点数量
 * @returns
 */
export const randomPointFeature = (map, count) => {
  const point = turf.randomPoint(count, {
    bbox: map.getView().calculateExtent(map.getSize()),
  });

  const features = new GeoJSON().readFeatures(point);

  return features;
};

/**
 * @abstract  生成当前地图范围内的随机线段数据
 * @param {*} map
 * @param {number} count  线段数量
 * @returns
 */
export const randomLineStringFeature = (map, count) => {
  const lineString = turf.randomLineString(count, {
    bbox: map.getView().calculateExtent(map.getSize()),
  });

  const features = new GeoJSON().readFeatures(lineString);

  return features;
};

export const randomPolygonFeature = (map, count) => {
  const lineString = turf.randomPolygon(count, {
    bbox: map.getView().calculateExtent(map.getSize()),
  });

  const features = new GeoJSON().readFeatures(lineString);

  return features;
};
